home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / VectorOfBool.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.3 KB  |  47 lines

  1. //: C20:VectorOfBool.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Demonstrate the vector<bool> specialization
  7. #include <iostream>
  8. #include <sstream>
  9. #include <vector>
  10. #include <bitset>
  11. #include <iterator>
  12. using namespace std;
  13.  
  14. int main() {
  15.   vector<bool> vb(10, true);
  16.   vector<bool>::iterator it;
  17.   for(it = vb.begin(); it != vb.end(); it++)
  18.     cout << *it;
  19.   cout << endl;
  20.   vb.push_back(false);
  21.   ostream_iterator<bool> out(cout, "");
  22.   copy(vb.begin(), vb.end(), out);
  23.   cout << endl;
  24.   bool ab[] = { true, false, false, true, true, 
  25.     true, true, false, false, true };
  26.   // There's a similar constructor:
  27.   vb.assign(ab, ab + sizeof(ab)/sizeof(bool));
  28.   copy(vb.begin(), vb.end(), out);
  29.   cout << endl;
  30.   vb.flip(); // Flip all bits
  31.   copy(vb.begin(), vb.end(), out);
  32.   cout << endl;
  33.   for(int i = 0; i < vb.size(); i++)
  34.     vb[i] = 0; // (Equivalent to "false")
  35.   vb[4] = true;
  36.   vb[5] = 1;
  37.   vb[7].flip(); // Invert one bit
  38.   copy(vb.begin(), vb.end(), out);
  39.   cout << endl;
  40.   // Convert to a bitset:
  41.   ostringstream os;
  42.   copy(vb.begin(), vb.end(), 
  43.     ostream_iterator<bool>(os, ""));
  44.   bitset<10> bs(os.str());
  45.   cout << "Bitset:\n" << bs << endl;
  46. } ///:~
  47.